package com.github.ghsea.scheduler.dto;

import java.io.Serializable;

/**
 * @author guhai
 * @since 2021/6/17 20:30
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -4956697445060644226L;

    private int code;
    private String message;
    private boolean success;
    private T data;

    public Result() {
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public boolean isSuccess() {
        return this.success = this.code == 0;
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        return new Result(0, "success", data);
    }

    public static <T> Result<T> error(int code, String message) {
        return error(code, message, null);
    }

    public static <T> Result<T> error(int code, String message, T data) {
        if (code == 0) {
            throw new IllegalArgumentException("r error code not allow " + code);
        } else {
            return new Result(code, message, data);
        }
    }

}
